var Cesium = require('cesium/Cesium');
require('./css/main.css');
require('cesium/Widgets/widgets.css');
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIzYThjMTI0OC0xOTE3LTRmNGEtYjZkNS02OWNmODNkYTA5MDEiLCJpZCI6NDQ4NjQsImlhdCI6MTYxNDUyMDg2OX0.JCjqt607bGjZFbxY4yAefUjZ1AGX0uj_M2jP5XsuAII';

var viewer = new Cesium.Viewer("cesiumContainer");
var scene = viewer.scene;

var tileset = scene.primitives.add(
    new Cesium.Cesium3DTileset({
        url: Cesium.IroResource.FromAssetId(8564)
    })
);

tileset.readyPromise.then(function (tileset) {
    viewer.zoomTo(tileset, new Cesium.HeadingPitchRange(
        0.5, -0.2, tileset.boundingSphere.radius * 4.0
    ));
})
.otherwise(function (error) {
    console.log(error);
});

tileset.colorBlendMode = Cesium.Cesium3DTileColorBlendMode.REPLACE;

var selectedFeature, picking = false;

function selectFeature(feature) {
    var element = feature.getProperty("element");
    setElementColor(element, Cesium.Color.YELLOW);
    selectedFeature = feature;
}

function unselectFeature(feature) {
    if (!Cesium.defined(feature)) {
        return;
    }

    var element = feature.getProperty("element");
    setElementColor(element, Cesium.Color.WHITE);
    if (feature === selectedFeature) selectedFeature = undefined;
}

function getElement(feature) {
    return parseInt(feature.getProperty("element"), 10);
}

function setElementColor(element, color) {
    var featuresToColor = elementMap[element];
    var length = featuresToColor.length;
    for (var i = 0; i < length; i++) {
        var feature = featuresToColor[i];
        feature.color = Cesium.Color.clone(color, feature.color);
    }
}


function unloadFeature(feature) {
    unselectFeature(feature);
    var element = getElement(feature);
    var features = elementMap[element];
    var index = features.indexOf(feature);
    if (index > -1) features.splice(index, 1);
}

function loadFeature(feature) {
    var element = getElement(feature);
    var features = elementMap[element];
    if (!Cesium.defined(features)) {
        features = [];
        elementMap[element] = features;
    }
    features.push(feature);

    if (hiddenElements.indexOf(element) > -1) feature.show = false;
}

function processContentFeatures(content, callback) {
    var featuresLength = content.featuresLength;
    for (var i = 0; i < featuresLength; i++) {
        var feature = content.getFeature(i);
        callback(feature);
    }
}

function processTileFeatures(tile, callback) {
    var content = tile.content;
    var innerContents = content.innerContents;
    if (Cesium.defined(innerContents)) {
        var length = innerContents.length;
        for (var i = 0; i < length; i++) processContentFeatures(innerContents[i], callback);
    }
    else {
        processContentFeatures(content, callback);
    }
}

tileset.tileLoad.addEventListener(function (tile) {
    processTileFeatures(tile, loadFeature);
})

tileset.tileUnload.addEventListener(function (tile) {
    processTileFeatures(tile, unloadFeature);
})